<?php
if (!defined('IN_DISCUZ')){
	exit('Access Denied');
}

global $_G;

$NoteToList = array($userID);

if ($uid && $_G['adminid'] != 1){
	showmessage('ban_khong_the_truy_xuat_thong_tin_gian_hang_nay', NULL, array(), array(
			'login'=>1
	));
}

if (!$uData)
	showmessage('ban_chua_dang_ky_gian_hang', 'shop.php?mod=store&ac=register');

if ($_G['adminid'] == 1 && $uid)
	$actionUrl = 'shop.php?mod=store&ac=profile&do=update&uid=' . $uid;
else
	$actionUrl = 'shop.php?mod=store&ac=profile&do=update';

$submited = submitcheck('submit', 1, $seccodecheck);

// Cac tinh/thanh cua VietNam
$provice = provice();

$uData['cardid_date'] = unserialize($uData['cardid_date']);
for ($i = 0; $i < count($uData['cardid_date']); $i++){
	$cardid_date = $uData['cardid_date']['bday'] . '/' . $uData['cardid_date']['bmonth'] . '/' . $uData['cardid_date']['byear'];
}

if ($_G['adminid'] == 1){
	if ($uData['verificationed'] == 1)
		$gp['verify'] = 1;

	$profile['verificationed'] = isset($_G['gp_verificationed']) ? intval($_G['gp_verificationed']) : 0;
	$profile['rate'] = isset($_G['gp_rate']) ? intval($_G['gp_rate']) : $uData['rate'];
}

$profile['sid'] = isset($_G['gp_sid']) ? intval($_G['gp_sid']) : $uData['sid'];

$profile['shopname'] = isset($_G['gp_shopname']) ? get_str_nohtml($_G['gp_shopname']) : $uData['shopname'];

$profile['authorname'] = isset($_G['gp_authorname']) ? get_str_nohtml($_G['gp_authorname']) : $uData['authorname'];

$profile['cardid_number'] = isset($_G['gp_cardid_number']) ? get_str_nohtml($_G['gp_cardid_number']) : $uData['cardid_number'];

$gp['reg_date_number'] = isset($_G['gp_reg_date_number']) ? get_str_nohtml($_G['gp_reg_date_number']) : $cardid_date;

$gp['date_number'] = explode('/', $gp['reg_date_number']);
for ($i = 0; $i < count($gp['date_number']); $i++){
	$gp['cardid_date']['bday'] = intval($gp['date_number'][0]);
	$gp['cardid_date']['bmonth'] = intval($gp['date_number'][1]);
	$gp['cardid_date']['byear'] = intval($gp['date_number'][2]);
}
$profile['cardid_date'] = addslashes(serialize($gp['cardid_date']));

$profile['cardid_issuer'] = isset($_G['gp_province']) ? intval($_G['gp_province']) : $uData['cardid_issuer'];
$profile['store_province'] = isset($_G['gp_store_province']) ? intval($_G['gp_store_province']) : $uData['store_province'];

$profile['shopaddr'] = isset($_G['gp_shopaddr']) ? get_str_nohtml($_G['gp_shopaddr']) : $uData['shopaddr'];

$profile['phonenumber'] = isset($_G['gp_phonenumber']) ? get_str_nohtml($_G['gp_phonenumber']) : $uData['phonenumber'];
$profile['mobile'] = isset($_G['gp_mobile']) ? get_str_nohtml($_G['gp_mobile']) : $uData['mobile'];
$profile['otherphone'] = isset($_G['gp_otherphone']) ? get_str_nohtml($_G['gp_otherphone']) : $uData['otherphone'];

$profile['email'] = isset($_G['gp_email']) ? get_str_nohtml($_G['gp_email']) : $uData['email'];

$profile['store_about'] = isset($_G['gp_store_about']) ? get_str_nohtml($_G['gp_store_about']) : $uData['store_about'];

$profile['bankid_issuer'] = isset($_G['gp_bankid_issuer']) ? get_str_nohtml($_G['gp_bankid_issuer']) : $uData['bankid_issuer'];

$profile['bankid_user'] = isset($_G['gp_bankid_user']) ? get_str_nohtml($_G['gp_bankid_user']) : $uData['bankid_user'];

$profile['bankid_number'] = isset($_G['gp_bankid_number']) ? get_str_nohtml($_G['gp_bankid_number']) : $uData['bankid_number'];

$profile['payment_other'] = isset($_G['gp_payment_other']) ? get_str_nohtml($_G['gp_payment_other']) : $uData['payment_other'];

$profile['deliveryinfor'] = isset($_G['gp_deliveryinfor']) ? get_str_nohtml($_G['gp_deliveryinfor']) : $uData['deliveryinfor'];

if ($submited){
	if (intval($profile['sid']) == 0)
		$post_erro['sid'] = 1;

	if (strlen($profile['shopname']) < 3)
		$post_erro['shopname'] = 1;

	if (strlen($profile['authorname']) < 7)
		$post_erro['authorname'] = 1;

	if (strlen($profile['cardid_number']) < 9)
		$post_erro['cardid_number'] = 1;

	if (strlen($gp['reg_date_number']) <= 0)
		$post_erro['reg_date_number'] = 1;

	if (strlen($profile['shopaddr']) < 20)
		$post_erro['shopaddr'] = 1;

	if (!isemail($profile['email']))
		$post_erro['email'] = lang('shop/template', 'erro_email');

	if (strlen($profile['bankid_issuer']) < 3)
		$post_erro['bankid_issuer'] = 1;

	if (strlen($profile['bankid_user']) < 8)
		$post_erro['bankid_user'] = 1;

	if (strlen($profile['bankid_number']) < 9)
		$post_erro['bankid_number'] = 1;

	if (strlen($profile['deliveryinfor']) < 8)
		$post_erro['deliveryinfor'] = 1;

	// Image
	if ($_FILES['profile_idcard']){
		$size = round(($_FILES['profile_idcard']['size'] / (1024)), 2);

		if ($size > 100)
			$post_erro['profile_idcard'] = lang('shop/template', 'idcart_img_is_maxsize');
	}

	if (empty($post_erro)){
		/////////////////////////////Xu ly phan hinh anh tai len////////////////////////////////////////
		require_once libfile('class/upload');
		$upload = new discuz_upload();

		if ($upload->init($_FILES['profile_idcard'], 'idcard') && $upload->save(1)){
			// Xoa anh cu
			if ($uData['profile_idcard'])
				unlink($uData['profile_idcard']);

			$profile['profile_idcard'] = $_G['setting']['attachurl'] . 'idcard/' . $upload->attach['attachment'];
			$gp['uploaded_idcard'] = true;
		}

		if ($gp['uploaded_idcard'] == false){
			$gp['delete_idcard'] = intval($_G['gp_delete_idcard']) == 1 ? 1 : 0;

			if ($gp['delete_idcard'] && $uData['profile_idcard']){
				unlink($uData['profile_idcard']);
				$profile['profile_idcard'] = '';
				// Deactive store
				$profile['verificationed'] = 0;
				//////////////////////////Gui thong diep toi user///////////////////////////////////
				notification_add($userID, 'icarddeleted', 'iCard_deleted', array(
						'actor'=>$_G['username']
				), 1);
			}
		}

		////////////////////////////////////////////////////////////////////////////////////////////
		if ($_G['adminid'] == 1 && $profile['verificationed'] == 1 && $uData['verificationed'] == 0){
			//Set user to shopping Group (Group ID = 20)
			if ($_G['gp_groupid'] == 20){
				$groupid = intval($_G['gp_groupid']);
				$query = DB::query("SELECT groupid FROM " . DB::table('common_usergroup') . " WHERE groupid NOT IN ('6', '7')");

				while ($group = DB::fetch($query)){
					if ($groupid == $group['groupid']){
						$membergoupt['groupid'] = $group['groupid'];
						DB::update('common_member', $membergoupt, array(
								'uid'=>$userID
						));
					}
				}
			}

			$note_array = array(
					'actor'=>$_G['username'],
					'url'=>'shop.php?mod=store&ac=post&do=new',
					'store_name'=>$profile['shopname']
			);
			foreach ($NoteToList as $key=>$touid){
				notification_add($touid, 'storeactived', 'store_actived', $note_array, 1);
			}
		}

		if ($profile['verificationed'] == 0 && $_G['adminid'] == 1){
			//Set user to new member Group (Group ID = 10)
			$query = DB::query("SELECT groupid FROM " . DB::table('common_usergroup') . " WHERE groupid NOT IN ('6', '7')");

			while ($group = DB::fetch($query)){
				if ($group['groupid'] == 10){
					$membergoupt['groupid'] = $group['groupid'];
					DB::update('common_member', $membergoupt, array(
							'uid'=>$userID
					));
				}
			}

			$dis_array = array(
					'actor'=>$_G['username'],
					'url'=>'shop.php?mod=store&ac=profile&do=update',
					'store_name'=>$profile['shopname']
			);
			foreach ($NoteToList as $key=>$touid){
				notification_add($touid, 'storedisable', 'store_disable', $dis_array, 1);
			}
		}
		////////////////////////////////////////////////////////////////////////////////////////////
		DB::update('shop_profile', $profile, array(
				'uid'=>$userID
		));
		///////////////////////////////////////////////////////////////////////////////////////////
		if ($_G['adminid'] == 1){
			$reUrl = 'shop.php?mod=store&ac=profile&do=update&uid=' . $userID;
		}else{
			$reUrl = 'shop.php?mod=store&ac=profile&do=update';

			$update_array = array(
					'actor'=>$_G['username'],
					'url'=>'shop.php?mod=store&ac=profile&do=update&uid=' . $userID,
					'store_name'=>$profile['shopname']
			);
			foreach ($NoteToAdmin as $key=>$touid){
				notification_add($touid, 'storeupdate', 'store_update', $update_array, 1);
			}
		}

		showmessage('ban_da_cap_nhat_gian_hang_thanh_cong_success', $reUrl);
	}
}

include template('shop/store/update_store');